﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entidades.Reclamos;
using WebApplication.Reclamos;

public partial class frmMtoReclamo : System.Web.UI.Page
{
    #region ATRIBUTOS

    protected IControllerRecReclamo _recController;
    protected DataTable dt {
                    get{
                        if(ViewState["dt_Reclamo"]==null){
                            ViewState["dt_Reclamo"] = new DataTable();
                        }
                        return (DataTable)ViewState["dt_Reclamo"];
                    }
                    set{
                        ViewState["dt_Reclamo"] = value;
                    }
    }
    protected RecReclamo criterio;

    #endregion

    #region CONSTRUCTOR

    public frmMtoReclamo() {
        _recController = new ControllerRecReclamo();
        criterio = new RecReclamo();
    }

    #endregion

    #region EVENTOS DEL FORMULARIO

    protected void Page_Load(object sender, EventArgs e)
    {
        verMensajes();
        if (!IsPostBack) {
            CargarLocal();
            CargarProducto();
            txtFechaInicio.Text = "01/01/2013";
            txtFechaFin.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now); 
        }
    }

    protected void CargarLocal()
    {
        List<GenLocal> lista = _recController.listarLocal();
        if (lista.Count > 0)
        {
            cmbLocal.DataSource = lista;
            cmbLocal.DataTextField = "nombre";
            cmbLocal.DataValueField = "idLocal";
            cmbLocal.DataBind();
            cmbLocal.Items.Insert(0, new ListItem(Constantes.STATUS_ALL, Constantes.CODE_SELECT));
        }
        else
        {
            cmbLocal.Items.Clear();
        }
    }

    protected void CargarProducto()
    {
        List<RecTipoBienContratado> lista = _recController.listarBienes();
        if (lista.Count > 0)
        {
            cmbProducto.DataSource = lista;
            cmbProducto.DataTextField = "descripcion";
            cmbProducto.DataValueField = "idTipoBien";
            cmbProducto.DataBind();
            cmbProducto.Items.Insert(0, new ListItem(Constantes.STATUS_ALL, Constantes.CODE_SELECT));
        }
        else
        {
            cmbProducto.Items.Clear();
        }
    }


    protected void verMensajes() {
        String msg = (Session["reclamo_rpta"] == null) ? "" : Session["reclamo_rpta"].ToString();        
        switch (msg) { 
            case "1":                
                Page.ClientScript.RegisterStartupScript(this.GetType(),"MSG_SAVE", "alert('" + Constantes.MSG_SAVE + "')", true);
                Session["reclamo_rpta"] = null;
                break;
            case "2":                
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_DELETE", "alert('" + Constantes.MSG_DELETE + "')", true);
                Session["reclamo_rpta"] = null;
                break;
            case "3":
                String msg_detail = (Session["reclamo_rpta_detail"] == null) ? "" : Session["reclamo_rpta_detail"].ToString();            
                msg_detail = msg_detail.Replace("\"","");
                msg_detail = msg_detail.Replace("'", "");
                msg_detail = msg_detail.Replace("\n", " ");
                msg_detail = msg_detail.Replace("\r", " ");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION1", "$('#mensaje').html('" + msg_detail + "'); ", true);
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('mensaje', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('" + Constantes.MSG_EXCEPTION +"', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);

                Session["reclamo_rpta"] = null;
                Session["reclamo_rpta_detail"] = null;


                break;
            default: 
                break;
        }        
    }

    protected void Listar() {
        List<RecReclamo> lista = _recController.listarReclamo(criterio);
        dt = new DataTable();
        dt.Columns.Add("Código");
        dt.Columns.Add("Local");
        dt.Columns.Add("Nombres");
        dt.Columns.Add("Descripción");
        dt.Columns.Add("Tipo");
        
        DataRow dr;
        foreach (RecReclamo be in lista)
        {
            dr = dt.NewRow();
            dr[0] = be.IdReclamo.ToString();
            dr[1] = be.Local.Nombre.ToString();
            dr[2] = be.Nombres.ToString();            
            dr[3] = be.Descripcion.ToString();
            dr[4] = be.NumeroTipo.ToString();          
            dt.Rows.Add(dr);
        }

        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    #endregion

    #region EVENTOS DE BOTONES

    protected void btnNuevo_Click(object sender, EventArgs e) {
        Session["reclamo_codigo"] = null;
        Session["reclamo_action"] = null;
        Session["reclamo_visible"] = null;
        Response.Redirect("frmReclamo.aspx");
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        System.Globalization.CultureInfo cultura = new System.Globalization.CultureInfo("en-GB");
        criterio = new RecReclamo();
        criterio.NumDNI = txtDNI.Text;
        criterio.IdReclamo = (txtCodigo.Text.Trim().Equals(""))?0:Int32.Parse(txtCodigo.Text);
        criterio.Descripcion = txtDescripcion.Text;
        criterio.Nombres = txtCliente.Text;
        criterio.NomProducto = cmbProducto.SelectedValue;
        criterio.Local.IdLocal = (cmbLocal.SelectedValue.Equals(""))?0: Int32.Parse(cmbLocal.SelectedValue);

        criterio.FecInicio = (txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlInicio = ((txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        criterio.FecFin = (txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlFin = ((txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        
        Listar(); 
    }

    #endregion

    #region EVENTOS DE GRID

    protected void gvCargos_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCargos.PageIndex = e.NewPageIndex;
        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    protected void verDetalle_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["reclamo_codigo"] = codigo;
        Session["reclamo_action"] = "visualizar";
        Session["reclamo_visible"] = false;
        Response.Redirect("frmReclamo.aspx");
    }

    protected void verModificar_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["reclamo_codigo"] = codigo;
        Session["reclamo_action"] = "modificar";
        Session["reclamo_visible"] = true;
        Response.Redirect("frmReclamo.aspx");
    }

    protected void verImprimir_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["reclamo_codigo"] = codigo;
        Session["reclamo_action"] = "imprimir";
        Session["reclamo_visible"] = true;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "DETALLE", "window.open('frmViewReclamo.aspx','Detalle del reclamo','height=650,width=450'); ", true);
    }

    protected void verEliminar_OnClick(object sender, CommandEventArgs e)
    {
        int codigo = (e.CommandName==null)?0:Convert.ToInt32(e.CommandName.ToString());
        int resultado = 0;        
        String StrResultado = "";
        try
        {            
            resultado = _recController.eliminarReclamo(codigo);
        }
        catch (Exception ex) {
            StrResultado = ex.Message.ToString();
        }

        if (StrResultado.Equals(""))
        {
            Session["reclamo_rpta"] = 2;
            Response.Redirect("frmMtoReclamo.aspx");
        }
        else {
            Session["reclamo_rpta"] = 3;
            Session["reclamo_rpta_detail"] = StrResultado;
            Response.Redirect("frmMtoReclamo.aspx");
        }
    }

    #endregion

}